<?php

declare(strict_types=1);
/**
 * 自定义标签处理
 * Author: Zane
 * Email: 873934580@qq.com
 * Date: 2022/4/12.
 */

namespace Zane\View\Providers;

use Illuminate\Support\Facades\Http;
use Zane\View\Compiler\ZaneCompiler;

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
    public function boot(): void
    {
        $this->registerCompiler();
        $this->registerHttp();
    }

    private function registerCompiler(): void
    {
        $this->app->singleton('blade.compiler', function ($app) {
            return new ZaneCompiler($app['files'], $app['config']['view.compiled']);
        });
    }

    private function registerHttp(): void
    {
        $result = ['get', 'put', 'post', 'head', 'patch', ' delete'];
        foreach ($result as $val) {
            if (Http::hasMacro($val)) {
                continue;
            }
            Http::macro($val, function ($url, ...$args) use ($val) {
                return tap(new PendingRequest($this), function ($request): void {
                    $request->stub($this->stubCallbacks);
                })->{$val}($url, ...$args);
            });
        }
    }
}
